﻿using AutoMapper;
using ManagementApi.Models;
using ManagementApi.Models.Manage.ProjectGroupMember;
using ManagementApi.Mvc;
using ManagementApi.Mvc.ApplicationContexts;
using ManagementApi.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules.Corp;
using Modules.Device;
using Modules.Person;
using Modules.Project;

namespace ManagementApi.Controllers.ManagerPannel
{
    /// <summary>
    /// 现场管理人员接口
    /// </summary>
    [Route("/api/v1/manager/personManagerEmployee")]
    [ApiController]
    [Authorize(Roles = "Manager")]
    public class PersonManagerEmployeeController : ControllerBase
    {
        private readonly ProjectGroupMemberService projectGroupMemberService;
        private readonly ProjectService projectService;
        private readonly ProjectGroupService projectGroupService;
        private readonly CorpService corpService;
        private readonly PersonService personService;
        private readonly PersonCorpContractService personCorpContractService;
        private readonly PublicPersonService publicPersonService;
        private readonly ProjectManagerApplicationContext applicationContext;
        private readonly CheckinDeviceService projectCheckinDeviceService;
        private readonly CheckinDeviceCommandService projectCheckinDeviceCommandService;
        private readonly PersonBankService personBankService;
        private readonly IMapper mapper;

        public PersonManagerEmployeeController(
            ProjectGroupMemberService projectGroupMemberService,
            ProjectService projectService,
            ProjectGroupService projectGroupService,
            CorpService corpService,
            PersonService personService,
            PersonCorpContractService personCorpContractService,
            PublicPersonService publicPersonService,
            ProjectManagerApplicationContext applicationContext,
            CheckinDeviceService projectCheckinDeviceService,
            CheckinDeviceCommandService projectCheckinDeviceCommandService,
            PersonBankService personBankService,
            IMapper mapper
            )
        {
            this.projectGroupMemberService = projectGroupMemberService;
            this.projectService = projectService;
            this.projectGroupService = projectGroupService;
            this.corpService = corpService;
            this.personService = personService;
            this.personCorpContractService = personCorpContractService;
            this.publicPersonService = publicPersonService;
            this.applicationContext = applicationContext;
            this.projectCheckinDeviceService = projectCheckinDeviceService;
            this.projectCheckinDeviceCommandService = projectCheckinDeviceCommandService;
            this.mapper = mapper;
        }

        /// <summary>
        /// 现场管理人员（分页）
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("page_managerEmployees")]
        public ApiResult<PageDto<UseEmployeeDto>> GetUseEmployees([FromQuery] ProjectGroupMemberQuery query, int pageIndex = 1, int pageSize = 20)
        {
            if (!query.ProjectId.HasValue) query.ProjectId = applicationContext.CurrentProjectInfo.ProjectId;
            query.IsManager = true;
            var data = projectGroupMemberService.GetProjectGroupMembersByManager(query, pageIndex, pageSize);
            var corps = corpService.GetCorpsByCorpIds(data.Select(a => a.CorpId));
            var projects = projectService.GetProjectInfoByProjectIds(data.Select(a => a.ProjectId));
            var persons = personService.GetPersons(data.Select(a => a.PersonId));
            var personCorpContracts = personCorpContractService.GetPersonCorpContractsByProjectIds(data.Select(a => a.ProjectId));
            var dtos = data.AsPageDto(source => { return source.AsManagerEmployeeDto(corps, projects, persons, personCorpContracts); });
            return ApiResult<PageDto<UseEmployeeDto>>.Success(dtos);
        }
    }
}